package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.entity.*;
import com.ruoyi.mall.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.service.IMallPackagesService;
import com.ruoyi.common.core.text.Convert;

/**
 * 卡包Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-05-25
 */
@Service
public class MallPackagesServiceImpl implements IMallPackagesService 
{
    @Autowired
    private MallPackagesMapper mallPackagesMapper;
    @Autowired
    private MallMembersMapper mallMembersMapper;
    @Autowired
    private MallMembersPackageMapper mallMembersPackageMapper;
    @Autowired
    private MallCouponsMapper mallCouponsMapper;
    @Autowired
    private MallPackageRecordMapper mallPackageRecordMapper;


    /**
     * 查询卡包
     * 
     * @param packageId 卡包ID
     * @return 卡包
     */
    @Override
    public MallPackages selectMallPackagesById(Long packageId)
    {
        return mallPackagesMapper.selectMallPackagesById(packageId);
    }

    /**
     * 查询卡包列表
     * 
     * @param mallPackages 卡包
     * @return 卡包
     */
    @Override
    public List<MallPackages> selectMallPackagesList(MallPackages mallPackages)
    {
        return mallPackagesMapper.selectMallPackagesList(mallPackages);
    }

    /**
     * 新增卡包
     * 
     * @param mallPackages 卡包
     * @return 结果
     */
    @Override
    public int insertMallPackages(MallPackages mallPackages)
    {
        mallPackages.setCreateTime(DateUtils.getNowDate());
        return mallPackagesMapper.insertMallPackages(mallPackages);
    }

    /**
     * 修改卡包
     * 
     * @param mallPackages 卡包
     * @return 结果
     */
    @Override
    public int updateMallPackages(MallPackages mallPackages)
    {
        mallPackages.setUpdateTime(DateUtils.getNowDate());
        return mallPackagesMapper.updateMallPackages(mallPackages);
    }

    /**
     * 删除卡包对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMallPackagesByIds(String ids)
    {
        return mallPackagesMapper.deleteMallPackagesByIds(Convert.toStrArray(ids));
    }
    /**
     * 购买优惠包
     */
    @Override
    public AjaxResult sellPackage(Long packageId, Long membersId) {
        MallMembers mallMembers = mallMembersMapper.selectMallMembersById(membersId);
        if(null == mallMembers){
            return  AjaxResult.warn("该会员不存在");
        }
        MallPackages mallPackages = mallPackagesMapper.selectMallPackagesById(packageId);
        if(null == mallPackages){
          return   AjaxResult.warn("该优惠包不存在");
        }
        //售卖时间段
        String sellTime = mallPackages.getSellTime();
        String[] split = sellTime.split(" - ");
        if(!DateUtils.ComparePeriod(split[0],split[1])){
            return   AjaxResult.warn("售卖时间已过期");
        }
        Long num = mallPackages.getNum();
        --num;
        if(num<=0){
            return  AjaxResult.warn("该优惠包数量已售完");
        }
        BigDecimal price = mallPackages.getPrice();
        BigDecimal money = mallMembers.getMoney();
        if(money.floatValue()<price.floatValue()){
            return  AjaxResult.warn("该会员账户余额不足");
        }
        BigDecimal subtract_money = money.subtract(price);
        mallMembers.setMoney(subtract_money);
        //更新账户
        mallMembersMapper.updateMallMembers(mallMembers);
        Long saleNum = mallPackages.getSaleNum();
        ++saleNum;
        mallPackages.setNum(num);
        mallPackages.setSaleNum(saleNum);
        //更新优惠包
        updateMallPackages(mallPackages);
        //新增用户优惠包记录
        MallMembersPackage mallMembersPackage = new MallMembersPackage();
        mallMembersPackage.setMembersId(membersId);
        mallMembersPackage.setPackageId(packageId);
        mallMembersPackage.setMoney(price.doubleValue());
        mallMembersPackage.setState(1L);
        mallMembersPackage.setCreateTime(DateUtils.getNowDate());
        //查询优惠包下的劵
        List<MallCoupons> mallCoupons = mallCouponsMapper.selectMallCouponsByPackageId(packageId);
        mallMembersPackage.setCouponStr(JSON.toJSONString(mallCoupons));
        mallMembersPackageMapper.insertMallMembersPackage(mallMembersPackage);
        //新增记录
        MallPackageRecord mallPackageRecord = new MallPackageRecord();
        mallPackageRecord.setMembersId(membersId);
        mallPackageRecord.setPackageId(packageId);
        mallPackageRecord.setMoney(price.doubleValue());
        mallPackageRecord.setCreateTime(DateUtils.getNowDate());
        return AjaxResult.success(mallPackageRecordMapper.insertMallPackageRecord(mallPackageRecord));
    }

    /**
     * 删除卡包信息
     * 
     * @param packageId 卡包ID
     * @return 结果
     */
    @Override
    public int deleteMallPackagesById(Long packageId)
    {
        return mallPackagesMapper.deleteMallPackagesById(packageId);
    }
}
